using Gtk;
using Gdk;
using Gee;
using WebKit;

namespace Meridian.Content {

    public class WebBook : Book<WebView, WebTab>, Notebook {
    
        public signal void load_started ();
        public signal void load_progress_changed (int progress);
        public signal void load_finished ();  
        public signal void title_changed (string title);
        
        private ArrayList<WebTab> tabs;
    
        public WebBook () {
            this.tabs = new ArrayList<WebTab> ();
        }
        
        public void add_tab (string title) {

            var tab = new WebTab (title, this);
            //connect signals
            WebView webview = tab.get_content ();
            webview.load_started.connect(load_started_manifold);
            webview.load_progress_changed.connect(load_progress_changed_manifold);
            webview.load_finished.connect(load_finished_manifold);
            webview.title_changed.connect((source, frame, title) => {
                title_changed_manifold (source, frame, title);
                tab.set_title(title);
            });
            webview.icon_loaded.connect((source, name) => {
                //Pixbuf pixbuf = webview.get_icon_pixbuf ();
                //tab.set_icon (pixbuf);
            });
                      
            this.tabs.add (tab);
            
            int page_number = this.append_page (tab.get_scrolled_window (), tab.get_label ());
            this.show_all ();
            this.set_current_page (page_number);  
        }
        
        public void close_tab (WebTab tab) {
            int clicked_tab = this.tabs.index_of (tab);
            this.tabs.remove(tab);
            this.remove_page (clicked_tab);
        }
        
        public WebView get_active_content () {
            return this.get_active_tab ().get_content ();       
        }
        
        private WebTab get_active_tab () {
            int active_page = this.get_current_page ();
            return this.tabs.get (active_page);
        }
        
        //web view methods
        public void go_to (string url) {
            this.get_active_content ().open (url);
        }
        
        public void go_back () {
            this.get_active_content ().go_back ();
        }
        
        public void go_forward () {
            this.get_active_content ().go_forward ();
        }
        
        public void reload () {
            this.get_active_content ().reload ();
        }
        
        public void stop_loading () {
            this.get_active_content ().stop_loading ();
        }
        
        //web view signal manifolds
        //only send signal for active tab
        private void load_started_manifold (WebView source, WebFrame frame) {
            if(source == this.get_active_content ()) {
                load_started ();
            }
        }
        
        private void load_progress_changed_manifold (WebView source, int progress) {
            if(source == this.get_active_content ()) {
                load_progress_changed ((int) progress);
            }
        }
        
        private void load_finished_manifold (WebView source, WebFrame frame) {
            if(source == this.get_active_content ()) {
                load_finished ();
            }
        }
        
        private void title_changed_manifold (WebView source, WebFrame frame, string title) {
            if(source == this.get_active_content ()) {
                title_changed (title);
            }
        }
    }
}
        
        